/*
 * Copyright (C) 2018 Logos Technology Ltd.
 */

package com.logos.tool.base64;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PushbackInputStream;

public class Base64Decoder extends CharacterDecoder {
    private static final char[] PEM_ARRAY = new char[]{'A', 'B', 'C', 'D',
            'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
            'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
            'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
            'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3',
            '4', '5', '6', '7', '8', '9', '+', '/'};
    private static final byte[] PEM_CONVERT_ARRAY = new byte[256];
    private byte[] mDecodeBuffer = new byte[4];

    static {
        int i;
        for (i = 0; i < 255; ++i) {
            PEM_CONVERT_ARRAY[i] = -1;
        }

        for (i = 0; i < PEM_ARRAY.length; ++i) {
            PEM_CONVERT_ARRAY[PEM_ARRAY[i]] = (byte) i;
        }

    }

    /**
     * bytesPerAtom
     *
     * @return int
     */
    protected int bytesPerAtom() {
        return 4;
    }

    /**
     * bytesPerLine
     *
     * @return int
     */
    protected int bytesPerLine() {
        return 72;
    }

    /**
     * decodeAtom
     *
     * @param inStream  inStream
     * @param outStream outStream
     * @param rem       rem
     * @throws IOException IOException
     */
    protected void decodeAtom(PushbackInputStream inStream,
                              OutputStream outStream, int rem) throws IOException {
        byte a0 = -1;
        byte b0 = -1;
        byte c0 = -1;
        byte d0 = -1;
        if (rem < 2) {
            throw new CeFormatException(
                    "BASE64Decoder: Not enough bytes for an atom.");
        } else {
            int i;
            do {
                i = inStream.read();
                if (i == -1) {
                    throw new CeStreamExhausted();
                }
            } while (i == 10 || i == 13);

            this.mDecodeBuffer[0] = (byte) i;
            i = this.readFully(inStream, this.mDecodeBuffer, 1, rem - 1);
            if (i == -1) {
                throw new CeStreamExhausted();
            } else {
                if (rem > 3 && this.mDecodeBuffer[3] == 61) {
                    rem = 3;
                }

                if (rem > 2 && this.mDecodeBuffer[2] == 61) {
                    rem = 2;
                }

                switch (rem) {
                    case 4:
                        d0 = PEM_CONVERT_ARRAY[this.mDecodeBuffer[3] & 255];
                    case 3:
                        c0 = PEM_CONVERT_ARRAY[this.mDecodeBuffer[2] & 255];
                    case 2:
                        b0 = PEM_CONVERT_ARRAY[this.mDecodeBuffer[1] & 255];
                        a0 = PEM_CONVERT_ARRAY[this.mDecodeBuffer[0] & 255];
                    default:
                        switch (rem) {
                            case 2:
                                outStream.write((byte) (a0 << 2 & 252 | b0 >>> 4 & 3));
                                break;
                            case 3:
                                outStream.write((byte) (a0 << 2 & 252 | b0 >>> 4 & 3));
                                outStream.write((byte) (b0 << 4 & 240 | c0 >>> 2 & 15));
                                break;
                            case 4:
                                outStream.write((byte) (a0 << 2 & 252 | b0 >>> 4 & 3));
                                outStream.write((byte) (b0 << 4 & 240 | c0 >>> 2 & 15));
                                outStream.write((byte) (c0 << 6 & 192 | d0 & 63));
                                break;
                            default:
                                break;
                        }

                }
            }
        }
    }
}
